<?php
if (isset($_SESSION['admin-login'])) {
	if (isset($_SESSION['status-pesanan'])) {
		echo "<p>".$_SESSION['status-pesanan']."</p>";
		unset($_SESSION['status-pesanan']);
	}
	if (isset($_SESSION['input-filter'])) {
		switch ($_SESSION['input-filter']) {
			case 0:
				$filter = "";
				$status_filter = 0;
				break;
			case 1:
				$filter = "WHERE status = 0";
				$status_filter = 1;
				break;
			case 2:
				$filter = "WHERE status = 1";
				$status_filter = 2;
				break;
			case 3:
				$filter = "WHERE status = 2";
				$status_filter = 3;
				break;
			case 4:
				$filter = "WHERE status = 3";
				$status_filter = 4;
				break;
			case 5:
				$filter = "WHERE status = 4";
				$status_filter = 5;
				break;
		}
		unset($_SESSION['input-filter']);
		$pesanan = db_array("SELECT * FROM pesanan $filter ORDER BY tgl DESC");
	} else if (isset($_SESSION['input-search'])) {
		$status_filter = 1;
		$nohp = $_SESSION['input-search'];
		unset($_SESSION['input-search']);
		$pesanan = db_array("SELECT * FROM pesanan WHERE kontak = '$nohp' ORDER BY tgl DESC");
	} else {
		$status_filter = 0;
		$pesanan = db_array("SELECT * FROM pesanan ORDER BY tgl DESC");
	}
	?>
	<form action="<?php echo site()."/".$param[0] ?>/pesanan-action" method="post">
		<select name="filter">
			<option value="0"<?php echo select_status(0,$status_filter) ?>>Semua</option>
			<option value="1"<?php echo select_status(1,$status_filter) ?>>Belum dibayar</option>
			<option value="2"<?php echo select_status(2,$status_filter) ?>>Sudah dibayar</option>
			<option value="3"<?php echo select_status(3,$status_filter) ?>>Sudah dikirim</option>
			<option value="4"<?php echo select_status(4,$status_filter) ?>>Sampai tujuan</option>
			<option value="5"<?php echo select_status(5,$status_filter) ?>>Batalkan</option>
		</select>
		<input type="submit" name="filtering" value="Filter" />
	</form>
	<form action="<?php echo site()."/".$param[0] ?>/pesanan-action" method="post">
		Search No HP : <input type="text" name="nohp" value="<?php if (isset($nohp)) echo $nohp; ?>" />
		<input type="submit" name="search" value="Search" />
	</form>
	<?php
	if ($pesanan == false) {
		echo "Tidak ada data";
	} else {
		?>
		<style type="text/css">
			table {
				border-collapse: collapse;
			}
			table,th,td {
				padding: 5px;
				border: 1px solid black;
			}
			.table-judul {
				font-weight: bold;
				text-align: center;
			}
		</style>
		<table><tbody>
			<tr>
				<td class="table-judul">Tanggal</td>
				<td class="table-judul">Nama</td>
				<td class="table-judul">Email</td>
				<td class="table-judul">Kontak</td>
				<td class="table-judul">Alamat</td>
				<td class="table-judul">Status</td>
				<td class="table-judul">Update status</td>
				<td class="table-judul">Hapus</td>
				<td class="table-judul">Detail</td>
				<!--------- DETAILL BEROOO !!!!! --->
			</tr>
			<?php
			$status[0] = "Belum dibayar";
			$status[1] = "Sudah dibayar";
			$status[2] = "Sudah dikirim";
			$status[3] = "Sampai tujuan";
			$status[4] = "Batalkan";
			foreach ($pesanan as $pes) {
				$transaksis = db_array("SELECT * FROM transaksi WHERE idorder = ".$pes['id']);
				?>
				<tr>
					<td><?php echo $pes['tgl'] ?></td>
					<td><?php echo $pes['nama'] ?></td>
					<td><?php echo $pes['email'] ?></td>
					<td><?php echo $pes['kontak'] ?></td>
					<td><?php echo $pes['alamat'] ?></td>
					<td><?php echo $status[$pes['status']] ?></td>
					<td>
						<form action="<?php echo site()."/".$param[0] ?>/pesanan-action" method="post">
							<input type="hidden" name="id" value="<?php echo $pes['id'] ?>" />
							<select name="status">
								<option value="0"<?php echo select_status(0,$pes['status']) ?>>Belum dibayar</option>
								<option value="1"<?php echo select_status(1,$pes['status']) ?>>Sudah dibayar</option>
								<option value="2"<?php echo select_status(2,$pes['status']) ?>>Sudah dikirim</option>
								<option value="3"<?php echo select_status(3,$pes['status']) ?>>Sampai tujuan</option>
								<option value="4"<?php echo select_status(4,$pes['status']) ?>>Batalkan</option>
							</select>
							<input type="submit" name="update" value="Update" />
						</form>
					</td>
					<td>
						<form action="<?php echo site()."/".$param[0] ?>/pesanan-action" method="post" onsubmit="return confirmHapus()">
							<input type="hidden" name="id" value="<?php echo $pes['id'] ?>" />
							<input type="submit" name="hapus" value="Hapus" />
						</form>
					</td>
					<td>
						<input type="submit" value="Detail" onclick="detailing(this,0,<?php echo count($transaksis) ?>,<?php echo $pes['id'] ?>)" />
					</td>
				</tr>
				<tr id="detail-head-<?php echo $pes['id']?>" style="display:none">
					<td colspan="5"></td>
					<td><strong>Nama produk</strong></td>
					<td><strong>Kuantitas</strong></td>
					<td><strong>Harga satuan</strong></td>
					<td><strong>Harga semua</strong></td>
				</tr>
				<?php
				$total_bayar = 0;
				foreach ($transaksis as $key => $transaksi) {
					$produk = db_result("SELECT * FROM produk WHERE id = ".$transaksi['idbarang']);
					$total_bayar += $produk['harga'] * $transaksi['kuantitas'];
					?>
					<tr id="detail-body-<?php echo $pes['id']?>-<?php echo $key ?>" style="display:none">
						<td colspan="5"></td>
						<td><?php echo $produk['nama'] ?></td>
						<td><?php echo $transaksi['kuantitas'] ?></td>
						<td><?php echo int_to_rp($produk['harga']) ?></td>
						<td><?php echo int_to_rp($produk['harga'] * $transaksi['kuantitas']) ?></td>
					</tr>
					<?php
				}
				?>
				<tr id="detail-foot-<?php echo $pes['id']?>" style="display:none">
					<td colspan="7"></td>
					<td><strong>Total bayar</strong></td>
					<td><?php echo int_to_rp($total_bayar) ?></td>
				</tr>
				<?php
			}
			?>
		</tbody></table>
		<script type="text/javascript">
			function confirmHapus() {
				return confirm("Anda yakin?");
			}
			function detailing(el,status,jml,id) {
				if (status == 0) {
					document.getElementById('detail-head-'+id).setAttribute("style","");
					document.getElementById('detail-foot-'+id).setAttribute("style","");
					for (i = 0; i < jml; i++) {
						document.getElementById('detail-body-'+id+'-'+i).setAttribute("style","");
					}
					el.setAttribute("onclick","detailing(this,1,"+jml+","+id+")");
					el.setAttribute("value","Close");
				} else if (status == 1) {
					document.getElementById('detail-head-'+id).setAttribute("style","display:none");
					document.getElementById('detail-foot-'+id).setAttribute("style","display:none");
					for (i = 0; i < jml; i++) {
						document.getElementById('detail-body-'+id+'-'+i).setAttribute("style","display:none");
					}
					el.setAttribute("onclick","detailing(this,0,"+jml+","+id+")");
					el.setAttribute("value","Detail");
				}
			}
		</script>
		<?php
	}
} else {
	$_SESSION['status-login'] = "Log in terlebih dahulu untuk melanjutkan";
	header("Location: ".site()."/".$param[0]."/login");
}
?>